using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using Peak.Can.Basic;
using TPCANHandle = System.Byte;
namespace WinCANTool
{
    public class NewMessage
    {

        private TPCANMsg CanMsg;
        private string DisplayString;
        private TPCANTimestamp MsgTimeStamp;
        private int Count = 0;
        private int SendRate = 0;
        private uint MessageDelta = 0;
        private bool MessageDisplayed = true;
        private bool TimeStampOn = false;
		public const int hw_vector_can = 0;
		public const int hw_pcan = 1;
		public const int hw_linde_canbox = 2;

        public NewMessage()
        {
            CanMsg = new TPCANMsg();
            CanMsg.DATA = new byte[8];
            MsgTimeStamp = new TPCANTimestamp();
        }

        public NewMessage(TPCANMsg Msg)
        {
            MsgTimeStamp = new TPCANTimestamp();
            CanMsg = new TPCANMsg();
            CanMsg.DATA= new byte[8];
            SaveID_DLC_Int(Msg.ID, Msg.LEN);
            CanMsg.MSGTYPE = Msg.MSGTYPE;
            for (int i = 0; i < 8; i++)
            {
                CanMsg.DATA[i] = Msg.DATA[i];
            }

        }


        public NewMessage(NewMessage MessageToCopy)
        {
            CanMsg = new TPCANMsg();
            MsgTimeStamp = new TPCANTimestamp();

            CanMsg.DATA = new byte[8];

            SaveID_DLC_Int(MessageToCopy.CanMsg.ID, MessageToCopy.CanMsg.LEN);
            //CanMsg.ID = MessageToCopy.CanMsg.ID;
            //CanMsg.Len = MessageToCopy.CanMsg.Len;
            CanMsg.MSGTYPE = MessageToCopy.CanMsg.MSGTYPE;

            this.DisplayString = MessageToCopy.DisplayString;
            this.MsgTimeStamp = MessageToCopy.MsgTimeStamp;
            this.Count = MessageToCopy.Count;
            this.SendRate = MessageToCopy.SendRate;
            this.MessageDisplayed = MessageToCopy.MessageDisplayed;

            for (int i = 0; i < 8; i++)
            {
                CanMsg.DATA[i] = MessageToCopy.CanMsg.DATA[i];
            }

        }


        public uint GetMessageDelta(NewMessage Msg1)
        {
            uint Delta;
            uint Msg1Time = Msg1.MsgTimeStamp.millis;
            uint Msg2Time = MessageDelta;

            Delta = Msg1Time - Msg2Time;
            return Delta;
        }

        public void ConvertNewMessage(TPCANMsg Msg)
        {
            SaveID_DLC_Int(Msg.ID, Msg.LEN);
            CanMsg.MSGTYPE = Msg.MSGTYPE;
            for (int i = 0; i < 8; i++)
            {
                CanMsg.DATA[i] = Msg.DATA[i];
            }

        }

        public void SaveTimeStamp(TPCANTimestamp tstamp)
        {
            MsgTimeStamp = tstamp;
            TimeStampOn = true;
        }


        public void SaveID_DLC(string IdentValue, byte dlc)
        {
            CanMsg.ID = (uint)Int32.Parse(IdentValue, NumberStyles.HexNumber);
            CanMsg.LEN = dlc;
        }

        public void SaveID_DLC_Int(uint IdentValue, byte dlc)
        {
            CanMsg.ID = IdentValue;
            CanMsg.LEN = dlc;

        }


        public void SaveData(int Index, string DataValueString)
        {
            byte DataValue = Byte.Parse(DataValueString, NumberStyles.HexNumber);
            CanMsg.DATA[Index] = DataValue;
        }

        public void SaveDataInt(int Index, byte DataValue)
        {
            CanMsg.DATA[Index] = DataValue;
        }


        public void SaveDisplayString(string NewString)
        {
            DisplayString = NewString;
        }

        public void SaveSendRate(int rate)
        {
            SendRate = rate;
        }

        public void SaveMessageDelta(uint delta)
        {
            MessageDelta = delta;
        }

        public void SaveMessageDisplayed(bool Displayed)
        {
            MessageDisplayed = Displayed;
        }

        public void SaveTimeStampOn(bool StampOn)
        {
            TimeStampOn = StampOn;
        }

        public TPCANMsg GetCANMsg()
        {
            return CanMsg;
        }

        public TPCANTimestamp GetTimeStamp()
        {
            return MsgTimeStamp;
        }

        public uint GetIdentifier()
        {
            return CanMsg.ID;
        }

        public byte GetDLC()
        {
            return CanMsg.LEN;
        }

        public TPCANMessageType GetMsgType()
        {
            return CanMsg.MSGTYPE;
        }

        public void GetData(ref byte[] Destination)
        {
            for (int i = 0; i < 8; i++)
            {
                Destination[i] = CanMsg.DATA[i];
            }
        }

        public byte GetDataValue(int index)
        {
            return CanMsg.DATA[index];
        }

        public int GetSendRate()
        {
            return SendRate;
        }

        public uint GetMessageDelta()
        {
            return MessageDelta;
        }

        public string GetDisplayString()
        {
            return DisplayString;
        }

        public bool GetMessageDisplayed()
        {
            return MessageDisplayed;
        }
        public bool GetTimeStampOn()
        {
            return TimeStampOn;
        }

        public int IncreaseCount()
        {
            Count++;
            return Count;
        }
        public int GetCount()
        {
            return Count;
        }

        public void ClearCount()
        {
            Count = 0;
        }

    }


    /***********    END Class New Message   *********************************************/    
    
    
    
    public class NewEventArgs : EventArgs
    {
        public NewMessage EventMessage = new NewMessage();

    }

    public delegate void NewMessageHandler(object CurrentForm, NewEventArgs Information);


    public class NewOptionsArgs : EventArgs
    {
        public CanToolOptionData Options = new CanToolOptionData();
    }

    //public class NewTrigOptionArgs : EventArgs
    //{
    //    public TriggerOptionsStruct OptionsStruct = new TriggerOptionsStruct();
    //}

    public delegate void NewMDIOptionsHandler(object CurrOb, NewOptionsArgs Info);
    //public delegate void TriggerOptionsHandler(object TrigOb, NewTrigOptionArgs Info);


    public class NodeFindArgs : EventArgs
    {
        public string NodeFindString;
        public string NodeDisplayString;
        public bool ReleaseThreadHalt;
        public int BaudParam;
        public int Progress;
    }

    public delegate void NodeFindHandler(object CurrentObject, NodeFindArgs Info);


    public struct Foundmsg
    {
        public int gridviewIndex;
        public int count;
        public uint DeltaTime;
        public bool Displayed;
    }

    
    
    public struct TraceOptionData
    {
        public bool DisplayAllMessages;
        public int LinesToDisplay;
        public bool IncludeAllLinesInSave;

    }


    public struct CanToolOptionData
    {
        //public bool ShowRawData;
        //public bool CanOpenOn;
        public bool HexDisplay;
        //public bool ErrorFrames;
        //public bool ShowPeriod;
        public bool UseCurrentTimeStamp;
        //public bool[] DataChange;
        //public string FileName;
        //public bool FactoryUser;
        //public int CaptureLimit;
        public int BaudIndex;
        //public TraceOptionData CaptureOptions;
        public string MainFileName;
        public string MainNodeID;
		public int hardware;
		public string port;
    }

    public struct Netparms
    {
        public TPCANHandle ClientHndl;
        public byte NetHndl;
        public byte HwHndl;
        public int Index;
        public uint CurrentBaudRate;
        public uint[] BaudRateValues;
        public string[] BaudRateStrings;
    }

    public struct MessageStrings
    {
        public string SubString;
        public string DataString;
        public string EnumString;

    }



    /**********************************************************************
     * Name:  CreateSymbolString
     * Purpose:  Creates a string that describes a message.
     * 
     * Input: NewMessage
     * Output: comma separated string containing message information.
     * *******************************************************************/
    
    public class CreateSymbolString
    {

        public Symbols SymbolRef;
        public byte[] Data = new byte[8];
        private long StartTimeTicks;


        public void SetTime(long TimeIn)
        {
            StartTimeTicks = TimeIn;
        }

        public CreateSymbolString()
        {
        }


        public NewMessage CreateNewMessage(string MsgString, Symbols SymList)
        {

            string[] SplitMsg = MsgString.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string TimeStamp = SplitMsg[0];
            string Ident = SplitMsg[1];
            string DataString = SplitMsg[2];
            string[] SplitData = DataString.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            TPCANMsg CanMsg = new TPCANMsg();
            CanMsg.DATA = new byte[8];
            SetIdent(Ident, ref CanMsg, SymList);
            for (int i = 0; ((i < 8) && (i < SplitData.Length)); i++)
            {
                CanMsg.DATA[i] = (byte)Int32.Parse(SplitData[i], NumberStyles.HexNumber);
            }
                        
            NewMessage Msg = new NewMessage(CanMsg);
            Msg.SaveDisplayString(TimeStamp);
            return Msg;

        }


        private void SetIdent(string Ident,ref TPCANMsg MsgIn, Symbols Syms)
        {
            //bool IdentFound = false;
          //  string TempString;
            int TempInt;
            uint TempIdent;

            Ident = Ident.TrimStart(new char[] {' ', '0', 'x', 'X' });
            if (Ident == "")
                TempIdent = 0;
            else
                TempIdent = (uint)Int32.Parse(Ident, NumberStyles.HexNumber);
            
            foreach (KeyValuePair<string, Message> MessageSymbol in Syms.NewSymbol)
            {
                TempInt = MessageSymbol.Value.Mask & (int) TempIdent;
                if (MessageSymbol.Value.IdentValue == TempInt)
                {
                    //IdentFound = true;
                    MsgIn.ID = (uint) TempIdent;
                    MsgIn.LEN = (byte) MessageSymbol.Value.Length;
                }
            }
        }





        /********************************************************************
         * Name: CreateSymbolStringFromMsg
         * Purpose: To create a string that will be displayed in the RxTx
         * window and the trace window.
         * Input: SymbolList,MsgIn, and DisplayOptions
         * Output: String with symbols if the identifer was found in the 
         * symbols, or a null string is returned if identifer is not found.
         *
         * Remaining change: Parse of bit data symbols.
         ********************************************************************/

        public string CreateSymbolStringFromMsg(Symbols SymbolList,NewMessage MsgIn,
            CanToolOptionData DisplayOps, bool TimeStampOn)
        {
            //string SymbolString = "";
            //string TempString = "";
            //string DateString = "";
            //string Tabs = "\t\t\t\t\t";
            //bool IdentFound = false;
            //SymbolRef = SymbolList;
            //int Ident = (int) MsgIn.GetIdentifier();
            //int TempInt;
            //int DataValue;
            //int DLC = MsgIn.GetDLC();
            //MsgIn.GetData(ref Data);


            ///******   Time Stamp  ******************************************************/

            //if (TimeStampOn)
            //{
            //    DateTime TempTime = GetTimeStampDateTime(MsgIn.GetTimeStamp());
            //    DateString = TempTime.ToString("MM dd yyyy hh:mm:ss.fff");
            //}
            //else
            //{
            //    if (DisplayOps.UseCurrentTimeStamp)
            //        DateString = MsgIn.GetDisplayString();
            //    else
            //        DateString = "Not Sent Yet";
            //}
            
            //SymbolString += DateString + ",\t";

            //if (MsgIn.GetMsgType() != CanApi2.MSGTYPE_ERRFRAME)
            //{

            //    /**********  Get Identifier Symbol ******************************************/

            //    foreach (KeyValuePair<string, Message> MessageSymbol in SymbolRef.NewSymbol)
            //    {
            //        TempInt = MessageSymbol.Value.Mask & Ident;
            //        if (MessageSymbol.Value.IdentValue == TempInt)
            //        {
            //            Tabs = "\t\t\t\t\t";
            //            IdentFound = true;
            //            TempString = MessageSymbol.Value.Name;
            //            if (TempString.Length < 8)
            //                SymbolString += TempString + ",\t\t";
            //            else
            //                SymbolString += TempString + ",\t";

            //            if (TempString.Length > 14)
            //                Tabs += '\t';

            //        }
            //    }
            //}
            //else
            //{
            //    SymbolString += "ErrorMsg" + ",\t";
            //    IdentFound = true;
            //}


            //if (IdentFound)
            //{

            //    /**********  Get All SubMessage Symbols *************************************/



            //    foreach (KeyValuePair<string, SubMessage> SubMessageTemp in SymbolRef.NewSubId)
            //    {
            //        //if (String.Compare(SubMessageTemp.Value.MessageId, TempString) == 0)
            //        //{
            //        //    SymbolString += SubMessageTemp.Value.SubMessageName + " = ";
            //        //    int SubMessageValue = GetSubmessageFromIdent(SubMessageTemp.Value, Ident);
            //        //    if(DisplayOps.HexDisplay)
            //        //        SymbolString += "0x" + SubMessageValue.ToString("X") + "\r\n" + Tabs;
            //        //    else
            //        //        SymbolString += SubMessageValue.ToString() + "\r\n" + Tabs;
            //        //}
            //    }


            //    /**********  Get All Data Symbols *****************************************/

            //    bool BigEndianType = false;
            //    bool SubIndexOn = false;
            //    int SubIndexValue = 0;
            //    string LinkedEnumName = "";

            //    foreach (KeyValuePair<string, DataWord> DataWordTemp in SymbolRef.NewData)
            //    {
            //        if (String.Compare(DataWordTemp.Value.MessageId, TempString) == 0)
            //        {
            //            SymbolString += DataWordTemp.Value.Name + " = ";
            //            if (DataWordTemp.Value.EndianType == "Big")
            //                BigEndianType = true;
            //            else
            //                BigEndianType = false;

            //            if (DataWordTemp.Value.SubIndexStartByte == -1)
            //                SubIndexOn = false;
            //            else
            //                SubIndexOn = true;

            //            if (DataWordTemp.Value.LinkedData == "none")
            //            {
            //                LinkedEnumName = DataWordTemp.Value.Name;
            //            }
            //            else
            //            {
            //                LinkedEnumName = DataWordTemp.Value.LinkedData;
            //            }

            //            //if (DisplayOps.CanOpenOn & !BigEndianType)
            //            //    DataValue = GetDataWordFromMessageLittleEnd(DataWordTemp.Value);
            //            //else
            //            //    DataValue = GetDataWordFromMessageBigEnd(DataWordTemp.Value);

            //            if (SubIndexOn)
            //            {
            //                SubIndexValue = GetSubIndex(DataWordTemp.Value);
            //                DataValue = (DataValue << 8) + SubIndexValue;
            //            }

            //            string TempEnumString = GetEnumString(LinkedEnumName, DataValue, SubIndexOn);

            //            if (TempEnumString == null)  /* If no Enum names are found */
            //            {
            //                if (DisplayOps.HexDisplay)
            //                    SymbolString += "0x" + DataValue.ToString("X") + "\r\n" + Tabs;
            //                else
            //                    SymbolString += DataValue.ToString() + "\r\n\t\t\t\t\t";
            //            }
            //            else                    /* Show Enum Index name       */
            //            {
            //                SymbolString += TempEnumString + "\r\n" + Tabs;

            //                if (SubIndexOn)
            //                {
            //                    SymbolString += DataWordTemp.Value.Name + "SubIndex = ";

            //                    if (DisplayOps.HexDisplay)
            //                        SymbolString += "0x" + SubIndexValue.ToString("X") + "\r\n" + Tabs;
            //                    else
            //                        SymbolString += SubIndexValue.ToString() + "\r\n\t\t\t\t\t";
            //                }
            //                //else
            //                    //SymbolString += Tabs;
            //            }
            //        }
            //    }



            //    /**********  Get All Bit Data Symbols ************************************/

            //    /******** not done yet *****/

            //    return SymbolString;
            //}

            return null;

        }


        /********************************************************************
         * Name: GetParsedMessage
         * Purpose: This routine is used to build strings that represent
         * symbols in a message that is being edited.
         * Input: Symbols, MessageData, Options, ref MessageStrings
         * Output: MsgStrings (structure that contains each of the strings to 
         * display showing symbols in edited message)
         *
         * Remaining change: BitData
         ********************************************************************/



        public void GetParsedMessage(Symbols SymbolList, MessageData CurrMessage, 
                                CanToolOptionData DisplayOps, ref MessageStrings MsgStrings)
        {

            SymbolRef = SymbolList;
            int DataValue;
            string TempString = CurrMessage.Name;
            int Ident = CurrMessage.GetIdentifier();
            CurrMessage.GetData(ref Data);
            ClearMsgSrings(ref MsgStrings);
            
            /**********  Get All SubMessage Symbols *************************************/
            foreach (KeyValuePair<string, SubMessage> SubMessageTemp in SymbolRef.NewSubId)
            {
                if (String.Compare(SubMessageTemp.Value.MessageId, TempString) == 0)
                {
                    MsgStrings.SubString += SubMessageTemp.Value.SubMessageName + " = ";
                    int SubMessageValue = GetSubmessageFromIdent(SubMessageTemp.Value, Ident);
                    if (DisplayOps.HexDisplay)
                        MsgStrings.SubString += "0x" + SubMessageValue.ToString("X") + "; ";
                    else
                        MsgStrings.SubString += SubMessageValue.ToString() + "; ";
                }
            }


            /**********  Get All Data Symbols *****************************************/
            //bool BigEndianType = false;
            bool SubIndexOn = false;
            int SubIndexValue = 0;
            string LinkedEnumName = "";

            
            foreach (KeyValuePair<string, DataWord> DataWordTemp in SymbolRef.NewData)
            {
                if (String.Compare(DataWordTemp.Value.MessageId, TempString) == 0)
                {
                    MsgStrings.DataString += DataWordTemp.Value.Name + " = ";
                   // if (DataWordTemp.Value.EndianType == "Big")
                   //     BigEndianType = true;
                   // else
                   //     BigEndianType = false;

                    if (DataWordTemp.Value.SubIndexStartByte == -1)
                        SubIndexOn = false;
                    else
                        SubIndexOn = true;

                    if (DataWordTemp.Value.LinkedData == "none")
                    {
                        LinkedEnumName = DataWordTemp.Value.Name;
                    }
                    else
                    {
                        LinkedEnumName = DataWordTemp.Value.LinkedData;
                    }

                    //if (DisplayOps.CanOpenOn & !BigEndianType)
                    //    DataValue = GetDataWordFromMessageLittleEnd(DataWordTemp.Value);
                    //else
                        DataValue = GetDataWordFromMessageBigEnd(DataWordTemp.Value);

                    if (SubIndexOn)
                    {
                        SubIndexValue = GetSubIndex(DataWordTemp.Value);
                        DataValue = (DataValue << 8) + SubIndexValue;
                    }


                    string TempEnumString = GetEnumString(LinkedEnumName, DataValue, SubIndexOn);

                    if (TempEnumString == null)  /* If no Enum names are found */
                    {
                        if (DisplayOps.HexDisplay)
                        {
                            MsgStrings.DataString += "0x" + DataValue.ToString("X") + "; ";
                            if (SubIndexOn)
                            {
                                MsgStrings.DataString += DataWordTemp.Value.Name + "SubIndex = ";
                                MsgStrings.DataString += "0x" + SubIndexValue.ToString("X") + "; ";
                            }
                        }
                        else
                        {
                            MsgStrings.DataString += DataValue.ToString() + "; ";
                            if (SubIndexOn)
                            {
                                MsgStrings.DataString += DataWordTemp.Value.Name + "SubIndex = ";
                                MsgStrings.DataString += SubIndexValue.ToString() + "; ";
                            }
                        }
                    }
                    else                    /* Show Enum Index name       */
                    {
                        MsgStrings.DataString += TempEnumString + "; ";


                        if (SubIndexOn)
                        {
                            MsgStrings.DataString += DataWordTemp.Value.Name + "SubIndex = ";

                            if (DisplayOps.HexDisplay)
                                MsgStrings.DataString += "0x" + SubIndexValue.ToString("X") + "; ";
                            else
                                MsgStrings.DataString += SubIndexValue.ToString() + "; ";
                        }

                    }
                }
            }



        }


        public void ClearMsgSrings(ref MessageStrings Strings)
        {
            Strings.DataString = "";
            Strings.SubString = "";
            Strings.EnumString = "";
        }



        /*******************************************************************
         * Name: GetSubmessageFromIdent
         * Purpose:  This routine is used to back out the submessage data
         * from a full identifer. 
         * 
         * Input: Submessage (Mask), Current Identifer
         * Output: Right justified submessage data
         * 
         * *****************************************************************/

        public int GetSubmessageFromIdent(SubMessage submsg, int Ident)
        {
            int Shifts, SubMessageTemp;
            int MaskAtLSB = submsg.Mask;
            int SubmessageValue;

            SubMessageTemp = Ident & submsg.Mask;
            /* Find how many shifts needed to Move mask to LSB */
            for (Shifts = 0; ((MaskAtLSB & 1) != 1); MaskAtLSB >>= Shifts, Shifts++) ;

            if (Shifts > 0)
            {
                SubmessageValue = SubMessageTemp >> (Shifts + 1);
            }
            else
                SubmessageValue = SubMessageTemp;

            return SubmessageValue;
        }


        /*******************************************************************
         * Name: GetDataWordFromMessage
         * Purpose:  This routine is used to back out the data
         * from a message. 
         * 
         * Input: DataWord, Current Identifer
         * Output: Right justified submessage data
         * 
         * *****************************************************************/


        private int GetDataWordFromMessageBigEnd(DataWord NewData)
        {
            int NewDataValue = 0;
            int NewStartByte = NewData.StartByte;
            int NewWordLength = NewData.WordLength;

            for (int k, i = 0; i < NewWordLength; i++)
            {
                k = NewStartByte + NewWordLength - i - 1;
                NewDataValue += (Data[k]) << (i * 8);
            }
            return NewDataValue;

        }



        private int GetDataWordFromMessageLittleEnd(DataWord NewData)
        {
            int NewDataValue = 0;
            int Shift = 0;
            int NewStartByte = NewData.StartByte;
            int NewWordLength = NewData.WordLength;

            for (int index, i = 0; i < NewWordLength; i++)
            {
                index = NewStartByte + NewWordLength - i - 1;
                if (NewWordLength > 1)
                    //Shift = ((i + ((i + 1) % 2)) * 8);
                    Shift = (( ((i + 1) % 2)) * 8);
                NewDataValue += (Data[index]) << Shift;
            }
            return NewDataValue;

        }


        private int GetSubIndex(DataWord NewData)
        {
            int IndexValue = 0;
            int NewStartByte = NewData.SubIndexStartByte;

            IndexValue = Data[NewStartByte];
            return IndexValue;
        }



        private string GetEnumString(string DataName, int DataWordValue, bool GetSubIndex)
        {
            int EnumIndex;
            int EnumSubIndex;


            foreach (KeyValuePair<string, EnumIndex> EnumTemp in SymbolRef.NewEnum)
            {
                if (String.Compare(EnumTemp.Value.DataWordName, DataName) == 0)
                {

                    if (GetSubIndex)
                    {
                        EnumSubIndex = EnumTemp.Value.Index & 0xFF;
                        EnumIndex = EnumTemp.Value.Index >> 8;
                    }
                    else
                    {
                        EnumIndex = EnumTemp.Value.Index;
                    }

                    if (EnumTemp.Value.Index == DataWordValue)
                    {
                        return EnumTemp.Value.Name;
                    }
                }
            }
            return null;

        }
        
        
        
        private DateTime GetTimeStampDateTime(TPCANTimestamp TimeIn)
        {
            long NewTime = 0;

            NewTime = StartTimeTicks + 10000 * (long)TimeIn.millis;
            DateTime TempTime = new DateTime(NewTime);
            return TempTime;
        }

    }




    
    /**********************************************************************
     * Name:  CreateRawMsgString
     * Purpose:  Creates a string that describes a message.
     * 
     * Input: NewMessage
     * Output: comma separated string containing raw message information.
     * *******************************************************************/
    
    public class CreateRawMsgString
    {

        private byte[] DataIn = new byte[8];
        private long StartTimeTicks;


        public void SetTime(long TimeIn)
        {
            StartTimeTicks = TimeIn;
        }

        public CreateRawMsgString()
        {
        }

        public string CreateRawString(ref NewMessage Msg, bool TimeStampOn, bool TriggerString)
        {
            int DLC;
            uint TempIdent;
            string DateString, tempString = "";

            /******   Time Stamp  *********************************/
            if (TimeStampOn)
            {
                DateTime TempTime = GetTimeStampDateTime(Msg.GetTimeStamp());
                DateString = TempTime.ToString("MM dd yyyy hh:mm:ss.fff");
            }
            else
                DateString = "Not Sent Yet";
            
            if (!TriggerString)
                tempString = DateString + " , ";

            tempString="";

            /******   Identifier  ***********************************/
            TempIdent = Msg.GetIdentifier();
            string temp = TempIdent.ToString("X");
            temp = temp.PadLeft(3, '0');
            tempString += "0x" + temp;



            /******   Raw Data Words  *******************************/
            Msg.GetData(ref DataIn);
            DLC = Msg.GetDLC();

			tempString += "    "+DLC.ToString();
			
            string tempdisplay = "";
            string tempdata = "";
            for (int i = 0; i < DLC; i++)
            {
                tempdata = DataIn[i].ToString("X");
                tempdata = tempdata.PadLeft(2, '0');
                if (TriggerString && (i == (DLC-1)))
                    tempdisplay += tempdata;
                else
                    tempdisplay += tempdata + " ";
            }
            if (!TriggerString)
                tempString += "    " + tempdisplay;
            else
                tempString += ", " + tempdisplay;

            return tempString;

        }

        private DateTime GetTimeStampDateTime(TPCANTimestamp TimeIn)
        {
            long NewTime = 0;

            NewTime = StartTimeTicks + 10000 * (long)TimeIn.millis;
            DateTime TempTime = new DateTime(NewTime);
            return TempTime;
        }


    }



}
